<?php
/**
 * @title 221. 最大正方形
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $matrix
     * @return Integer
     */
    function maximalSquare($matrix) {
        /**
         * @since 2024-01-02 行数和列数
         */
        $row = count($matrix);
        $col = count($matrix[0]);

        /**
         * @since 2024-01-02 一行内的元素累加
         */
        for($r=0; $r<$row; $r++){
            /**
             * @since 2024-01-02 上一个数
             */
            $prevNum = 0;

            for($c=0; $c<$col; $c++){
                /**
                 * @since 2024-01-02 连续1，则累计
                 */
                if($matrix[$r][$c] == 1){
                    $matrix[$r][$c] = $prevNum+1;
                } else {}

                /**
                 * @since 2024-01-02 更新上一个数
                 */
                $prevNum = $matrix[$r][$c];
            }
        }

        // /**
        //  * @since 2024-01-02 输出matrix
        //  */
        // for($r=0; $r<$row; $r++){
        //     echo implode(",", $matrix[$r]), PHP_EOL;
        // }

        /**
         * @since 2024-01-02 最大正方形的边长
         */
        $maxSquareSideLength = 0;

        /**
         * @since 2024-01-02 查找最大的正方形
         */
        for($r=0; $r<$row; $r++){
            for($c=0; $c<$col; $c++){
                /**
                 * @since 2024-01-02 元素值
                 */
                $num = $matrix[$r][$c];

                /**
                 * @since 2024-01-02 存在同列，上一行元素
                 */
                if($r>0){
                    /**
                     * @since 2024-01-02 <=上一行元素，剪枝
                     */
                    $prevNum = $matrix[$r-1][$c];
                    if($num <= $prevNum){
                        continue;
                    } else {}
                } else {}

                /**
                 * @since 2024-01-02 向下找正方形
                 */
                for($i=1; $i<=$num; $i++){
                    /**
                     * @since 2024-01-02 同列，下一行元素
                     */
                    $nextR = $r+$i-1;
                    if($nextR < $row){
                        /**
                         * 1 2 3    1 2 3
                         * 1 2 3    0 1 2
                         * 1 2 3    1 2 3
                         */
                        $nextNum = $matrix[$nextR][$c];

                        /**
                         * @since 2024-01-02 更新最大边长
                         */
                        if($nextNum >= $i){
                            $maxSquareSideLength = max($maxSquareSideLength, $i);
                            $num = min($num, $nextNum);
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                // echo "num={$num}, [{$r}, {$c}], maxSquareSideLength=", $maxSquareSideLength, PHP_EOL;
            }
        }

        /**
         * @return Integer
         */
        return pow($maxSquareSideLength, 2);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.02_10.php
 */
$datas = [
    // [[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]], // 4
    // [[["0","0","0","1"],["1","1","0","1"],["1","1","1","1"],["0","1","1","1"],["0","1","1","1"]]], // 9
    // [[["1","0","1","1","0","1"],["1","1","1","1","1","1"],["0","1","1","0","1","1"],["1","1","1","0","1","0"],["0","1","1","1","1","1"],["1","1","0","1","1","1"]]], // 4
    [[["0","1","1","0","1","1","1","1","1","1","1","1","1","1","1","1","1","1","0"],["1","0","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","0","1","1","1","1","1","1","1","1","1","0","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1"],["1","1","1","0","1","1","1","0","1","1","1","1","1","1","1","1","1","0","1"],["1","0","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","0"],["0","0","1","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1"],["1","1","0","1","1","1","1","1","1","1","0","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","0","1","1","1","1","1","1","1","1","1"],["0","1","1","0","1","1","1","0","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","0","1","1","1","1","1","1","1","1","1","0","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","0","1"],["1","1","1","1","1","1","1","1","0","1","1","0","1","1","0","1","1","1","1"],["1","1","1","1","1","1","0","1","1","1","1","1","1","1","1","0","1","1","1"]]], // 25
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maximalSquare(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
